package com.sunny.ihealth.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TextInputLayout;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sunny.ihealth.BaseActivity;
import com.sunny.ihealth.R;
import com.sunny.ihealth.data.BmobManager;
import com.sunny.ihealth.data.PreferencesHelper;
import com.sunny.ihealth.entity.app.SPUserInfo;
import com.sunny.ihealth.entity.bmob.UserInfo;
import com.sunny.ihealth.utils.MyTextUtils;
import com.sunny.ihealth.utils.NetworkUtils;

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private TextInputLayout textInputLayoutName, textInputLayoutPwd;
    private EditText etUsername, etPassword;
    private Button btnLogin;

    //从主页来
    private boolean fromMain = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Intent intent = getIntent();
        if (intent != null) {
            fromMain = intent.getBooleanExtra("fromMain", false);
        }
        initViews();
    }

    @Override
    public void initViews() {
        setEnableBackIcon(true);
        setStatusBarColor();
        setToolbar("登陆");

        textInputLayoutName = (TextInputLayout) findViewById(R.id.textInputLayout_login_username);
        textInputLayoutPwd = (TextInputLayout) findViewById(R.id.textInputLayout_login_password);
        etUsername = textInputLayoutName.getEditText();
        etPassword = textInputLayoutPwd.getEditText();
        btnLogin = (Button) findViewById(R.id.btn_login_login);

        btnLogin.setOnClickListener(this);

        SPUserInfo spUserInfo;
        //游客模式，有用户资料，说明之前登陆过，但是登陆失败
        if (getPreferencesHelper().getMode() == PreferencesHelper.MODE_VISITOR && (spUserInfo = getPreferencesHelper().getSPUserInfo()) != null) {
            etUsername.setText(spUserInfo.getUsername());
            etPassword.setText(spUserInfo.getPassword());
        }
    }

    @Override
    public void onClick(View v) {
        if (!NetworkUtils.isNetworkAvailable(LoginActivity.this, true)) {
            return;
        }
        String username = etUsername.getText().toString().toLowerCase();
        String password = etPassword.getText().toString();
        if (!MyTextUtils.isLength(username, 3, 16)) {
            Toast.makeText(LoginActivity.this, "账号格式有误，长度应在3-16位~", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!MyTextUtils.isLength(password, 6, 16)) {
            Toast.makeText(LoginActivity.this, "密码格式有误，长度应在6-16位~", Toast.LENGTH_SHORT).show();
            return;
        }
        btnLogin.setEnabled(false);
        login(username, password);
    }

    //登陆
    private void login(final String username, final String password) {
        getBmob().login(username, password, new BmobManager.Action1<UserInfo>() {
            @Override
            public void onCompleted(UserInfo userInfo) {
                btnLogin.setEnabled(true);
                if (userInfo != null) {
                    getPreferencesHelper().setMode(PreferencesHelper.MODE_REGISTER_USER);
                    getPreferencesHelper().setUserAccount(username, password);
                    getPreferencesHelper().setUserInfo(userInfo);

                    //不是来自主页，则显示主页
                    if (!fromMain) {
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                    } else {  //更新首页信息
                        getBaseApp().setUpdateState(true);
                    }
                    setResult(Activity.RESULT_OK);
                    finish();
                    //Toast.makeText(LoginActivity.this, "登陆成功~", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


}
